package com.xbkj.pgrp.server.model.common;
import org.springframework.stereotype.Service;
import xyz.erupt.jpa.dao.EruptDao;

import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;

@Service
public class WorkdayCalculator {
    @Resource
    private EruptDao eruptDao;

    // 节假日集合（非工作日）
    private static final Set<LocalDate> holidays = new HashSet<>();
    // 调休工作日集合（周末但需要上班）
    private static final Set<LocalDate> extraWorkdays = new HashSet<>();
    /**
     *  计算两个日期之间的工作日天数
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 工作日天数
     */
    public long calculateWorkdays(LocalDate startDate, LocalDate endDate) {
        if (startDate.isAfter(endDate)) {
            throw new IllegalArgumentException("开始日期不能晚于结束日期");
        }

        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate) + 1;
        long workdays = 0;

        for (int i = 0; i < daysBetween; i++) {
            LocalDate currentDate = startDate.plusDays(i);
            if (isWorkday(currentDate)) {
                workdays++;
            }
        }

        return workdays;
    }

    /**
            * 判断某天是否是工作日
     * @param date 日期
     * @return true-工作日，false-非工作日
     */
    private  boolean isWorkday(LocalDate date) {
        // 首先检查是否是调休工作日
        if (extraWorkdays.contains(date)) {
            return true;
        }

        // 检查是否是节假日
        if (holidays.contains(date)) {
            return false;
        }

        // 检查是否是周末
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        return dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY;
    }

    /**
            * 添加节假日
     */
    private void loadHoliday() {
//        holidays.add(date);
    }

    /**
            * 添加调休工作日
     */
    public  void loadExtraWorkday() {
//        extraWorkdays.add(date);
    }


}
